/*
 * Copyright 2008-2009 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public Licence Version 3
 * The licence can be read in its entirety in the LICENSE.txt file accompaning this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of WeightWatch.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package net.cachapa.weightwatch.ui;

import com.sun.lwuit.Button;
import com.sun.lwuit.Command;
import com.sun.lwuit.Container;
import com.sun.lwuit.Dialog;
import com.sun.lwuit.Image;
import com.sun.lwuit.Label;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.BoxLayout;
import com.sun.lwuit.plaf.UIManager;
import net.cachapa.weightwatch.util.L;

public class DatabasePreferencesContainer extends Container implements PreferencesContainer, ActionListener {
	private Application application;
	private String name;
	private Image icon;
	private Label databaseSizeLabel;
	private Button viewDatabaseButton;
	private Button regenerateTrendButton;
	private Button deleteDatabaseButton;
	
	public DatabasePreferencesContainer(Application application) {
		this.application = application;
		this.setLayout(new BoxLayout(BoxLayout.Y_AXIS));
		this.name = L.l("pref_tab_database");
		try {
			this.icon = Image.createImage("/database.png");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		
		// Information on the database
		this.databaseSizeLabel = new Label();
		this.addComponent(databaseSizeLabel);
		
		// View the database items in a list
		this.viewDatabaseButton = new Button("database_viewdatabase");
		this.viewDatabaseButton.setAlignment(CENTER);
		this.viewDatabaseButton.addActionListener(this);
		this.addComponent(viewDatabaseButton);
		
		// Delete the entire database
		this.deleteDatabaseButton = new Button("database_deletedatabase");
		this.deleteDatabaseButton.setAlignment(CENTER);
		this.deleteDatabaseButton.addActionListener(this);
		this.addComponent(deleteDatabaseButton);
	}
	
	public void show() {
		UIManager ui = UIManager.getInstance();
		viewDatabaseButton.setEnabled(true);
		deleteDatabaseButton.setEnabled(true);
		int dbSize = application.getWeightWatch().getFullDatabase().size();
		String dbSizeText;
		dbSizeText = ui.localize("database_size", "database_size") + ": " + dbSize;
		this.databaseSizeLabel.setText(dbSizeText);
	}

	public void actionPerformed(ActionEvent ae) {
		Object source = ae.getSource();
		if (source == viewDatabaseButton) {
			if (application.getWeightWatch().getFullDatabase().size() == 0) {
				Dialog.show("error", "database_databaseempty", new Command[] {new Command("ok")},
					 Dialog.TYPE_INFO, null, 0);
			}
			else {
				application.performAction(Application.SHOW_DATABASE_LIST_FORM);
			}
		}
		
		else if (source == regenerateTrendButton) {
			application.getWeightWatch().regenerateTrendValues();
		}
		
		else if (source == deleteDatabaseButton) {
			boolean result = Dialog.show("database_deletemessagetitle", "database_deletemessagebody",
				Dialog.TYPE_WARNING ,null, "Poof!", "cancel");
			if (result) {
				application.getWeightWatch().deleteDatabase();
				application.performAction(Application.UPDATE_CHARTS);
				show();
			}
		}
	}

	public String getName() {
		return name;
	}

	public Image getIcon() {
		return icon;
	}
}
